package testes_unitarios;

import static org.junit.Assert.*;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.*;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import mock.MockCadastroClienteServlet;
import model.Cliente;

import org.junit.Test;

import dao.ClienteDAO;

public class TestCadastroClienteServlet {
	
	private MockCadastroClienteServlet servlet = new MockCadastroClienteServlet();
	
	//@Test
	public void doPostTest() throws ServletException, IOException {
		
		HttpServletRequest request = mock(HttpServletRequest.class);
		HttpServletResponse response = mock(HttpServletResponse.class);
		HttpSession sessao = mock(HttpSession.class);
		RequestDispatcher rq = mock(RequestDispatcher.class);
		
		when(request.getSession()).thenReturn(sessao);
		
		when(request.getParameter("nome")).thenReturn("Felipe");
		when(request.getParameter("cpf")).thenReturn("25037641853");
		when(request.getParameter("telefone")).thenReturn("25697459");
		when(request.getParameter("endereco")).thenReturn("Rua Abreu Vieira, 497");
		when(request.getParameter("email")).thenReturn("felipe@gmail.com");
		when(request.getParameter("senha")).thenReturn("123456");
		
		when(request.getRequestDispatcher(anyString())).thenReturn(rq);
		doNothing().doThrow(new RuntimeException()).when(rq).forward(request, response);
		
		servlet.doPost(request, response);
		
		ClienteDAO cdao = new ClienteDAO();
		Cliente cliente = cdao.recuperarCliente("felipe@gmail.com", "123456");
		
		assertEquals("Felipe", cliente.getNome());
		
		sessao.invalidate();
		
	}
	
	@Test
	public void validaDadosTest() {
		
		Cliente cliente = new Cliente("Marcos", "12345678912", "4864215", "teste", "teste", "123456");
		HttpServletRequest request = mock(HttpServletRequest.class);
		
		Boolean dadosInvalidos = servlet.validaDados(cliente, request);
		
		// para testar erro, basta colocar algum cpf com menos de 11 caracteres
		assertEquals(false, dadosInvalidos);
	}

}
